/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.user.service.mapper;

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.user.domain.YxDistributorEarnings;
import co.yixiang.modules.user.service.dto.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
* @author yhh
* @date 2020-09-16
*/
@Repository
public interface YxDistributorEarningsMapper extends CoreMapper<YxDistributorEarnings> {

    /**
     * 获取用户佣金
     *
     * @param uid
     * @return
     */
    @Select("SELECT yu.uid , yu.brokerage_price as brokeragePrice, \n" +
            "IFNULL((SELECT SUM(extract_price) FROM yx_user_extract where type = 2 and uid = #{uid}),0) as exPrice , \n" +
            "IFNULL((SELECT SUM(price) FROM yx_distributor_earnings where is_over = 1 and status = 1 and uid = #{uid}),0) as receivedPrice ,  \n" +
            "IFNULL((SELECT SUM(price) FROM yx_distributor_earnings where is_over = 0 and status = 1 and uid = #{uid}),0)  as uncollectedPrice from yx_user yu where yu.uid = #{uid}")
    UsePriceDTO getUserPrice(@Param("uid") Long uid);


    /**
     * 获取用户下级列表
     * @param pageModel
     * @param uid
     * @return
     */
    @Select("<script>SELECT a1.* , (SELECT COUNT(1) from yx_distributor_relation where p_uid = a1.uid) as juniorNum from (SELECT uid , nickname , avatar , brokerage_price as brokeragePrice FROM yx_user where uid in (SELECT uid from yx_distributor_relation where p_uid = #{uid})) a1 </script>")
    IPage<JuniorUserDataDTO> getJuniorData(@Param("pageModel") Page<JuniorUserDataDTO> pageModel, @Param("uid") Long uid);

    /**
     * 获取对应的佣金金额
     * @param uid
     * @param type
     * @return
     */
    @Select("SELECT IFNULL(SUM(price) , 0) FROM yx_distributor_earnings where is_over = #{type} and uid = #{uid} and status = 1")
    BigDecimal getPriceByType(@Param("uid") Long uid,@Param("type") Integer type);

    /**
     * 获取一级用户对我的收益
     * @param uid
     * @param dUid
     */
    @Select("SELECT IFNULL(SUM(price) , 0) from yx_distributor_earnings where uid = #{uid} and d_uid = #{dUid}")
    BigDecimal getOneDownPrice(@Param("uid") Long uid, @Param("dUid") Long dUid);

    /**
     * 获取下级用户对我的收益
     * @param uid
     * @param dUid
     */
    @Select("SELECT IFNULL(SUM(price) , 0) from yx_distributor_earnings where uid = #{uid} and d_uid in (select uid from yx_distributor_relation where p_uid  = #{dUid}) and is_over = 1 and status = 1")
    BigDecimal getDownPrice(@Param("uid") Long uid, @Param("dUid") Long dUid);

    /**
     *
     * @param pageModel
     * @param uid
     * @param value
     * @return
     */
    @Select("SELECT yde.d_uid as uid , yde.price , yde.type ,yde.is_over , yde.create_time , yu.avatar , yu.nickname from yx_distributor_earnings yde, yx_user yu where yu.uid = yde.d_uid  and yde.is_over = #{isOver} and yde.`status` = 1 and yde.uid = #{uid} order by yde.create_time desc")
    IPage<DistributorDetail> getDistributorDetail(@Param("pageModel") Page<DistributorDetail> pageModel,@Param("uid")  Long uid,@Param("isOver")  Integer isOver);

    @Select("SELECT a1.* , (SELECT COUNT(1) from yx_distributor_relation where p_uid = a1.uid) as juniorNum from (SELECT uid , nickname , avatar , brokerage_price as brokeragePrice FROM yx_user where uid = #{otherUid}) a1")
    JuniorUserDataDTO getJuniorDataByUid(@Param("otherUid") Long otherUid);

    @Select("SELECT yde.d_uid as uid , yde.price , yde.type ,yde.is_over , yde.create_time , yu.avatar , yu.nickname from \n" +
            "yx_distributor_earnings yde, yx_user yu where yu.uid = yde.d_uid \n" +
            "and yde.`status` = 1 and yde.uid = #{uid} and (yde.d_uid = #{otherUid} or yde.d_uid in (SELECT uid from yx_distributor_relation ydr where ydr.p_uid = #{otherUid})) order by yde.create_time desc")
    IPage<DistributorDetail> getOtherDistributorDetail(@Param("pageModel") Page<DistributorDetail> pageModel, @Param("uid") Long uid,@Param("otherUid")  Long otherUid);

    @Select("select a.* , b.nickname as uName from yx_distributor_earnings a,yx_user b where a.uid = b.uid order by a.create_time desc")
    List<YxDistributorEarningsDto> selectEarinDto(@Param("criteria") YxDistributorEarningsQueryCriteria criteria);
}
